package com.dongdongshop.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.dongdongshop.data.PageResult;
import com.dongdongshop.data.PageUtil;
import com.dongdongshop.domain.model.TbBrand;
import com.dongdongshop.domain.model.TbBrandExample;
import com.dongdongshop.domain.vo.BrandVO;
import com.dongdongshop.mapper.TbBrandMapper;
import com.dongdongshop.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private TbBrandMapper brandMapper;

    @Override
    public PageUtil<BrandVO> brandList(PageResult<BrandVO> parms) {
        BrandVO parm = parms.getParm();
        TbBrandExample te = new TbBrandExample();
        TbBrandExample.Criteria criteria = te.createCriteria();
        //判断name是否为空
        if (StringUtils.isNotBlank(parm.getName())){
            criteria.andNameLike("%"+parm.getName()+"%");
        }
        //判断firstChar是否为空
        if (StringUtils.isNotBlank(parm.getFirstChar())){
            criteria.andFirstCharEqualTo(parm.getFirstChar());
        }
        PageHelper.startPage(parms.getPageNum(), parms.getPageSize());
        List<TbBrand> tbBrandList = brandMapper.selectByExample(te);
        PageInfo info = new PageInfo(tbBrandList);
        List<BrandVO> brandVOList = tbBrandList.stream().map(brand -> {
            BrandVO vo = new BrandVO();
            BeanUtils.copyProperties(brand,vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageUtil<>(brandVOList, info.getTotal(), parms.getPageNum(), parms.getPageSize());
    }

    @Override
    public void addBrand(BrandVO brand) {
        TbBrand tbBrand = new TbBrand();
        BeanUtils.copyProperties(brand,tbBrand);
        brandMapper.insertSelective(tbBrand);
    }

    @Override
    public BrandVO selectById(Long id) {
        TbBrand tbBrand = brandMapper.selectByPrimaryKey(id);
        BrandVO brandVO = new BrandVO();
        BeanUtils.copyProperties(tbBrand,brandVO);
        return brandVO;
    }

    @Override
    public void updateBrand(BrandVO brand) {
        TbBrand tbBrand = new TbBrand();
        BeanUtils.copyProperties(brand,tbBrand);
        brandMapper.updateByPrimaryKeySelective(tbBrand);
    }

    @Override
    public void delete(Long id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteBrand(List<Long> id) {
        TbBrandExample tbBrandExample = new TbBrandExample();
        tbBrandExample.createCriteria().andIdIn(id);
        brandMapper.deleteByExample(tbBrandExample);
    }

    @Override
    public List<BrandVO> getAllBrand() {
        List<TbBrand> tbBrandList = brandMapper.selectByExample(null);
        List<BrandVO> brandVOList = tbBrandList.stream().map(tbBrand -> {
            BrandVO brand = new BrandVO();
            BeanUtils.copyProperties(tbBrand,brand);
            return brand;
        }).collect(Collectors.toList());
        return brandVOList;
    }
}
